﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using YEngineLib;
using YEngineLib.Animation;

using YEngineLib;

namespace Spyfall.Objects
{
    class Birds : Object
    {
        public Texture2D tex;
        public Texture2D texReversed;
        List<Animation> obstacleAnimationList;
        Animation idleAnimation;

        //public Vector2 origin;
        public Vector2 position;
        public Vector2 velocity;

        int leftOrRightSpawn;

        public Birds()
        {
            ObjectID = Object_Type.BIRDS;

            //// Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            //texture = YEngine.Game.Content.Load<Texture2D>("Graphics/obstacle01");
            //obstacleAnimationList = new List<Animation>();
            //idleAnimation = new Animation();
            //idleAnimation.Initialize(texture, position, 94, 60, 2, 0.1f, Color.White, 1, true);
            //obstacleAnimationList.Add(idleAnimation);
            //// Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        }

        public void Initialize(Texture2D texture, float positionX, float positionY, float velocityX, float velocityY)
        {

            tex = texture;

            obstacleAnimationList = new List<Animation>();
            idleAnimation = new Animation();

            position.Y = positionY;
            velocity.Y = velocityY;

            Random rand = new Random();
            if (rand.Next(1, 100) >= 50)
            {
                texReversed = YEngine.Game.Content.Load<Texture2D>("Graphics/birdsanireversed");
                //leftOrRightSpawn = -1;
                position.X = 1300;
                velocity.X = -velocityX;
                idleAnimation.Initialize(texReversed, position, 117, 117, 2, 0.2f, Color.White, 1, true);
            }
            else
            {
                //leftOrRightSpawn = 1;
                position.X = positionX;
                velocity.X = velocityX;
                idleAnimation.Initialize(tex, position, 117, 117, 2, 0.2f, Color.White, 1, true);
            }






            obstacleAnimationList.Add(idleAnimation);

            // ColRect = new Rect(position.X, position.Y, texture.Bounds.Left, texture.Bounds.Top);
            ColRect = new Rectangle((int)position.X, (int)position.Y, this.tex.Width / 2, this.tex.Height);
        }

        public override void Update(float deltaTime)
        {
            //// Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            for (int i = obstacleAnimationList.Count - 1; i >= 0; i--)
            {
                obstacleAnimationList[i].Update(deltaTime);

                //MOVEMENT
               // position.Y += (velocity.Y * deltaTime);
                obstacleAnimationList[i].Position.X += (velocity.X * deltaTime);

                if (obstacleAnimationList[i].Active == false)
                {
                    obstacleAnimationList.RemoveAt(i);
                }
            }
            //// Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //MOVEMENT
            //position.Y += (velocity.Y * deltaTime);
            position.X += (velocity.X * deltaTime);
            ColRect = new Rectangle((int)position.X -45, (int)position.Y -40, this.tex.Width / 2 - 20, this.tex.Height - 30);

        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            //// Animation Stuff Start ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            for (int i = 0; i < obstacleAnimationList.Count; i++)
            {
                obstacleAnimationList[i].Draw(spriteBatch);
            }
            //// Animation Stuff End   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            //YEngine.Graphics.SpriteBatch.Draw(tex, position, Color.White);
            //YEngine.Graphics.SpriteBatch.Draw(tex, position, null, Color.White, 360, 0, SpriteEffects.None, 0);


            // draw collision rects
            Texture2D dummyTexture;
            Rectangle dummyRectangle;
            dummyRectangle = this.ColRect;
            dummyTexture = new Texture2D(YEngine.Game.GraphicsDevice, 1, 1);
            dummyTexture.SetData(new Color[] { Color.White });


            //YEngine.Graphics.SpriteBatch.Draw(dummyTexture, dummyRectangle, Color.White);

        }

        public override bool Collide(Object tar)
        {
            bool res = ColRect.Intersects(tar.ColRect);
            return res;
        }
    }
}

